using UnityEngine;
using System.Collections;

public class Sword : MonoBehaviour {
	
	public Transform handRef;
	private float jabDuration = Mathf.Infinity; //0.2f;
	private float depth = 0.6f;
	private GameObject mousePointer;
	private Vector3 attackPos ; 
	private bool onJab = false;
	private Quaternion originalAngle;
	private Vector3 originalPos;
	private Vector3 originalPoint;
	private Transform background;
	private Vector3 backgroundOriginalPos;
	private Transform sword;
	private float jabTime = 0;
	private MouseLook[] mLooks;
	
	

	// Use this for initialization
	void Start () {
		mousePointer = GameObject.FindGameObjectWithTag("mousePointer");
		mLooks = GameObject.FindObjectsOfType(typeof(MouseLook)) as MouseLook[];
		background =  ((MousePointer)mousePointer.GetComponent<MousePointer>()).background.transform;
		backgroundOriginalPos = background.localPosition;
		//originalAngle = rigidbody.transform.rotation;
		attackPos = new Vector3(0,0,depth);
		originalPos = transform.localPosition;
		sword = transform.GetChild(0);
		//rigidbody.freezeRotation = true;
	}
	
	// Update is called once per frame
	void Update () {
		//transform.LookAt(mousePointer.transform);
		if(isDefense()){
			onDefense();
		}else if(Mouse.isClick())
			startJab();
		else{
			if(onJab){
				jabFinish();
			}
			/*transform.localPosition = originalPos;
		
			transform.LookAt(handRef);*/
			transform.rotation = handRef.rotation;
			transform.position = handRef.position;
			background.localPosition= backgroundOriginalPos;
		}
		/*if(Mouse.onClick()){
			audio.Play();
		}*/
		
	}
	
	private void startJab(){
		
		transform.LookAt(mousePointer.transform);
		//if(!onJab)
			//transform.position  += transform.forward*depth;
			transform.localPosition =handRef.position + getForward();
		if(!onJab){
			jabTime = Time.time + jabDuration;
			/*foreach (MouseLook m in mLooks){
				m.sensitivityX /= 2;
				m.sensitivityY /= 2;
			}*/
		}
		onJab = true;
		cutSound();
	}
	
	private void jabFinish(){
		onJab = false;
		transform.localPosition = originalPos;
		
		/*foreach (MouseLook m in mLooks){
			m.sensitivityX *= 2;
			m.sensitivityY *= 2;
		}*/
		
		//transform.rotation =  originalAngle;
	}
	
	private Vector3 getForward(){
		Vector3 temp = (mousePointer.transform.position - transform.position).normalized;
		if(Time.time <= jabTime){
			temp *= depth;
		}else{
			jabTime = 0;
			temp *= getDepth(); //0.01f;
			//jabFinish();
		}
		return temp;
	}
	
	private void cutSound(){
		float temp = Mathf.Abs(Input.GetAxis ("Mouse X") + Input.GetAxis ("Mouse Y"));
		print("t"+temp+"   v"+rigidbody.velocity.magnitude);
		if(temp >= 0.05f && transform.rigidbody.velocity.magnitude > 0.000008f){
			if(!audio.isPlaying){
				audio.Play();
				//print("play");
			}
		}
	}
	
	private float getDepth(){
		// float temp = Mathf.Abs(Input.GetAxis ("Mouse X") + Input.GetAxis ("Mouse Y"));
		float temp = rigidbody.velocity.magnitude;
		// print(temp);
		if(temp > 0.000008f)
			return depth;
		else return 0;
		
	}
	
	private bool isDefense(){
		return Mouse.isRightHold();
	}
	
	private void onDefense(){
		//background.localPosition = backgroundOriginalPos+  new Vector3(0f,0f,-1.6f);
		background.position = handRef.position;
		transform.position = handRef.position;
		
		//transform.rotation = Quaternion.identity;
		////transform.lorotation. = new Quaternion(-90, 0, 0,0);
		
	/*	// Print the rotation around the global X Axis
print (transform.eulerAngles.x); 
// Print the rotation around the global Y Axis
print (transform.eulerAngles.y); 
// Print the rotation around the global Z Axis
print (transform.eulerAngles.z); */
		//transform.eulerAngles = new Vector3(270,-90,0);
		
		transform.rotation = handRef.rotation;
		transform.LookAt(mousePointer.transform);
		transform.Rotate(new Vector3(-90,0,0));
		
		
	    transform.position = mousePointer.transform.position - (sword.position - transform.position);
		
		/*transform.position = handRef.position;
		transform.LookAt(transform.position + getAngle());
		transform.Rotate(new Vector3(90,0,0));*/
		//transform.LookAt(mousePointer.transform);
		//transform.localPosition = originalPos + getForward();
		
	}
	
	private Vector3 getAngle(){
		Vector3 a = Vector3.zero;
		a.x = Input.mousePosition.x - Screen.width/2;
		a.y = Input.mousePosition.y - (Screen.height/2) + 200;
		a = a*1000000;
		a.z = 0.3f;
		return a;
	}
	
	private float getRotation(){
		float size = Screen.width/2;
		float cos = (Input.mousePosition.x - size)/size;
		if(cos > size)
			cos = size;
		else if (cos < -size)
			cos = -size;
		float angle = Mathf.Acos(cos)*90;
		Debug.Log( Time.time+" c="+cos + "acos="+ angle);
		return angle;
	}
	
	
}
